package cn.yh.register.user.service.impl;

import cn.yh.register.client.cmn.CmnDictClient;
import cn.yh.register.enums.DictEnum;
import cn.yh.register.model.user.Patient;
import cn.yh.register.user.mapper.PatientMapper;
import cn.yh.register.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    CmnDictClient cmnDictClient;
    /**
     * 获取某用户的就诊人列表
     *
     * @param userId 用户id
     * @return 就诊人列表
     */
    @Override
    public List<Patient> findUserById(Long userId) {
        List<Patient> patients = baseMapper.selectList(new LambdaQueryWrapper<Patient>().eq(Patient::getUserId, userId));
        patients.forEach(this::packPatient);
        return patients;
    }

    /**
     * 根据id获取就诊人信息
     *
     * @param id 就诊人id
     * @return 就诊人信息
     */
    @Override
    public Patient getPatientId(Long id) {
        Patient patient = baseMapper.selectById(id);
        packPatient(patient);
        return patient;

    }

    //
    private void packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                cmnDictClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        String contactsCertificatesTypeString =
                cmnDictClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = cmnDictClient.getName(patient.getProvinceCode());
        //市
        String cityString = cmnDictClient.getName(patient.getCityCode());
        //区
        String districtString = cmnDictClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

    }
}
